package com.sky.controller.admin;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Api("管理端订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索接口
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("统计各个状态的订单数");
        return Result.success(orderService.statistics());
    }

    /**
     * 查询订单详情
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("查询订单详情:{}",id);
        return Result.success(orderService.details(id));
    }

    /**
     * 商家接单
     * @return
     */
    @PutMapping("confirm")
    @ApiOperation("接单接口")
    public Result confirm(@RequestBody Orders orders) {
        log.info("接单成功:{}",orders);
        orderService.confirm(orders);
        return Result.success();
    }

    /**
     * 拒单
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单接口")
    public Result rejection(@RequestBody Orders orders) throws Exception {
        log.info("拒单：{}",orders);
        orderService.rejection(orders);
        return Result.success();
    }

    /**
     * 取消订单接口
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单接口")
    public Result cancel(@RequestBody Orders orders) throws Exception {
        log.info("取消订单:{}",orders);
        orderService.cancelByAdmin(orders);
        return Result.success();
    }

    /**
     * 派送订单
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery(@PathVariable Long id) {
        log.info("派送订单:{}",id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单接口")
    public Result complete(@PathVariable Long id) {
        log.info("完成订单:{}",id);
        orderService.complete(id);
        return Result.success();
    }
}
